var GetInfo = {

	realtimeSearch : function() {
		var counterName = Ext.getCmp("counterName").getValue();
		var interval = Ext.getCmp("interval").getValue();
		Ext.getCmp("chartPanel1").getStore().removeAll();
		Ext.getCmp("chartPanel2").getStore().removeAll();
		Ext.getCmp("chartPanel3").getStore().removeAll();
		var msg = this.chackRelValue(counterName, interval);
		if (msg != "") {
			Ext.Msg.alert("提示", msg);
			return;
		}
		GetInfo.getRealtimeInfo("getRealtimeAllInfo", vmname,
				counterName, interval);
		
		var info = "主机：" + vmname.split('(')[0]+ ",参数：" + counterName + ",取点间隔：" + interval + "秒";
		Ext.getCmp("panel1").setTitle(info + "的CPU实时监控信息");
		Ext.getCmp("panel2").setTitle(info + "的内存实时监控历史信息");
		Ext.getCmp("panel3").setTitle(info + "的硬盘实时监控历史信息");
	},

	chackRelValue : function(counterName, interval) {
		if (counterName == "")
			return "计数参数不能为空";
		if (interval == "")
			return "取点间隔不能为空";
		return "";
	},

	historySearch : function() {
		var starttime = Ext.getCmp("starttime").getValue();
		var counterName = Ext.getCmp("counterName").getValue();
		var duration = Ext.getCmp("duration").getValue();
		var interval = Ext.getCmp("interval").getValue();
		Ext.getCmp("chartPanel1").getStore().removeAll();
		Ext.getCmp("chartPanel2").getStore().removeAll();
		Ext.getCmp("chartPanel3").getStore().removeAll();
		var msg = this.chackHisValue(starttime, counterName,duration,interval);
		if (msg != "") {
			Ext.Msg.alert("提示", msg);
			return;
		}
		GetInfo.getHistoryInfo("getHistoryAllInfo", vmname, starttime,
				counterName, duration, interval);
		
		var info = "主机：" + vmname.split('(')[0]+ ",参数" + counterName + ",查询区间：" + duration + "分" + "，前"
				+ starttime + "分取点"
		Ext.getCmp("panel1").setTitle(info + "的CPU历史监控信息");
		Ext.getCmp("panel2").setTitle(info + "的内存历史监控信息");
		Ext.getCmp("panel3").setTitle(info + "的硬盘历史监控信息");
	},
	
	chackHisValue : function(starttime, counterName,duration,interval) {
		if (starttime == "")
			return "开始时间不能为空";
		if (counterName == "")
			return "计数参数不能为空";
		if (duration == "")
			return "持续时间不能为空";
		if (interval == "")
			return "取点间隔不能为空";
		if(duration>starttime)
		{
			return "持续时间有误，持续时间不能大于开始时间";
		}
		return "";
	},

	getHistoryInfo : function(handleName, vmname, starttime, counterName,
			duration, interval) {
		var query = new Query('HistoryResourcesMonitor',
				'HistoryResourcesMonitor');
		query.setHandlerMethod(handleName);
		query.addCondition("vmname", vmname);
		query.addCondition("starttime", starttime);
		query.addCondition("counterName", counterName);
		query.addCondition("duration", duration);
		query.addCondition("interval", interval);
		query.setCompletedFun(GetInfo.afterloadInfo);
		query.setCompletedFunScope(GetInfo);
		query.load();
	},

	getRealtimeInfo : function(handleName, vmname, counterName, interval) {
		var query = new Query('RealtimeResourcesMonitor',
				'RealtimeResourcesMonitor');
		query.setHandlerMethod(handleName);
		query.addCondition("vmname", vmname);
		query.addCondition("counterName", counterName);
		query.addCondition("interval", interval);
		query.setCompletedFun(GetInfo.afterloadInfo);
		query.setCompletedFunScope(GetInfo);
		query.load();
	},

	afterloadInfo : function(success, dataset, response, opts) {
		
		if (success) {
			if (dataset && !Ext.isEmpty(dataset.record)) {
				this.dataset = dataset;
				for(var i =0 ; i <dataset.record.length;i++ )
				{
					switch(dataset.record[i].type)
					{
					case "cpu": 
						Ext.getCmp("chartPanel1").getStore().add(dataset.record[i]);
						break;
					case "memory":
						Ext.getCmp("chartPanel2").getStore().add(dataset.record[i]);
						break;
					case "disk":
						Ext.getCmp("chartPanel3").getStore().add(dataset.record[i]);
						break;
					}
				}				
			}else{
				Ext.Msg.alert("提示","查询不到监控数据");
			}
		} else {
			
		}
	}
};
